package 树;

/**
 * @author ssh
 * @date 2025/8/17
 * @description
 */
public class 翻转二叉树 {
    public TreeNode invertTree(TreeNode root){
        if (root==null){
            return  null;
        }
        TreeNode tmp= root.left;
        root.left=root.right;
        root.right= tmp;
        invertTree(root.left);
        invertTree(root.right);
        return  root;
    }

    public static void main(String[] args) {
        // 测试代码
        TreeNode root = new TreeNode(1);
        root.left = new TreeNode(2);
        root.right = new TreeNode(3);
        root.left.left = new TreeNode(4);
        root.left.right = new TreeNode(5);

        翻转二叉树 solution = new 翻转二叉树();
        TreeNode invertedRoot = solution.invertTree(root);

        // 输出翻转后的树结构
        System.out.println(invertedRoot.val); // 输出: 1
        System.out.println(invertedRoot.left.val); // 输出: 3
        System.out.println(invertedRoot.right.val); // 输出: 2
    }

}
